#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.
#************************BEGIN Add Include Files to Path************************

PricesFileName='/home/robyg/Desktop/prices.csv'


TotalInstallPrice=[0]
TotalRemovePrice=[0]

import sys
IncludePath="/opt/coopgis/djangoproject/include"
if(not IncludePath in sys.path):
    sys.path.append(IncludePath)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
import csv
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopgis/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject.settings
setup_environ(djangoproject.settings)
from djangoproject.models import *

#********************Setup psycopg2 and django section********************


#Data Structure example:  
#Dictionary['A1'] = [RemovePrice, InstallPrice]
def ReadPrices(FileName):
    Prices={}
    File=open(FileName, 'rb')
    reader=csv.reader(File)
    Rows=[]
    for row in reader:
        Rows.append(row)
    File.close()
    for row in Rows:
        if(not Prices.has_key(row[0])):
            Prices[row[0]] = [0,0]
    for row in Rows:
        if len(row) > 1:
            if(row[1]):
                try:
                    Prices[row[0]][0] = int(row[1])
                except:
                    Prices[row[0]][0] = 0
        if len(row) > 2:
            if(row[2]):
                try:
                    Prices[row[0]][1] = int(row[2])
                except:
                    Prices[row[0]][1] = 0
    return Prices

def GetInstallAssemblies():
    """Returns Assemblies in Format Results['CompatibleUnitName']= Quantity"""
    Results={}
    Resules['1/0 ACSR'] = 0.504
    Results['2 TRI'] = 0.131
    Results['4 ACSR'] = 0.0010
    Results['477 ACSR'] = 20297
    Results['A5E'] = 1
    Results['A5-1E'] = 1
    Results['A5-2E'] = 2
    Results['A5-3E'] = 1
    Results['A5-3EX'] = 2
    Results['C1-2'] = 4
    Results['C1-3'] = 13-1
    Results['C4-1E'] = 2
    Results['C4-1EX'] = 2
    Results['C5-1E'] = 1
    Results['C7-1E'] = 2
    Results['C9-2X'] = 1
    Results['E1-2'] = 5+3
    Results['E1-3'] = 28-3+1
    Results['E13'] = 2
    Results['E2-2GL'] = 1
    Results['E2-3GL'] = 2
    Results['E3-10'] = 26 + 1
    Results['F1-2S'] = 1
    Results['F1-3S'] = 19
    Results['F1-5S'] = 2
    Results['G10'] = 3
    Results['G15'] = 5
    Results['J6'] = 3
    Results['K14C'] = 4
    Results['M2-11'] = 15
    Results['M2-12'] = 4
    Results['M5-10'] = 2+1
    Results['M5-27'] = 23+1+1+2
    Results['M5-28'] = 2+2+1
    Results['VC2V(S)'] = 3
    Results['P40-4'] = 8
    Results['P45-3'] = 11
    Results['M5-9T'] = 9
    Results['M5-5'] = 2
    Results['M5-4'] = 3
    Results['M42-11'] = 2
    Results['C1-3X'] = 1
    return Results
    

def GetRemoveAssemblies():
    """Returns Assemblies in Format Results['CompatibleUnitName']= Quantity"""
    Results={}
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    Results[''] =
    return Results


def GetInstallPrice(Prices, CompatibleUnitName, Quantity):
    if(Prices.has_key(CompatibleUnitName)):
        if(Prices[CompatibleUnitName][1]):
            TotalInstallPrice[0]=TotalInstallPrice[0] + Quantity * int(Prices[CompatibleUnitName][1]) 
            return str(int(Quantity * Prices[CompatibleUnitName][1]))
        else:
            return 'No Install Price'
    else:
        return 'No Match For Unit'

def GetRemovePrice(Prices, CompatibleUnitName, Quantity):
    if(Prices.has_key(CompatibleUnitName)):
        if(Prices[CompatibleUnitName][0]):
            TotalRemovePrice[0]=TotalRemovePrice[0] + Quantity * int(Prices[CompatibleUnitName][0])
            return str(int(Quantity * Prices[CompatibleUnitName][0]))
        else:
            return 'No Remove Price'
    else:
        return 'No Match For Unit'
    




#Need to access a Staker Work Order

Prices=ReadPrices(PricesFileName)
InstallList = GetInstallAssemblies()
RemoveList = GetRemoveAssemblies()
print "**************************Install Costs**************************"
for i in InstallList:
    Quantity=InstallList[i]
    if('.' in str(Quantity)):
        Quantity=round(Quantity, 3) 
    print i + ', $' + GetInstallPrice(Prices, i, InstallList[i]) + ',' + str(Quantity)
print "\n\n\n\n" 
print "**************************Remove Costs**************************"
print
for i in RemoveList:
    Quantity=RemoveList[i]
    if('.' in str(Quantity)):
        Quantity=round(Quantity, 3) 
    print i + ', $' + GetRemovePrice(Prices, i, RemoveList[i]) + ',' + str(Quantity)

print "Total Install Cost:  " + str(round(TotalInstallPrice[0], 2)) +\
      "   Total Remove Cost:  " + str(round(TotalRemovePrice[0], 2)) +\
      "  Total:  " + str(round(TotalInstallPrice[0] + TotalRemovePrice[0], 2))

